package com.saturday.redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.MasterSlaveServersConfig;
import org.redisson.config.ReadMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@ConditionalOnProperty(prefix = "base.cache", name = "redission", havingValue = "true")
public class BaseRedis {

    @Value("${redis.mode:master-slave}")
    private String redisMode;
    @Value("${redis.node.address}")
    private String redisAddress;
    @Value("${redis.node.port}")
    private String redisPort;
    @Value("${redis.node.password}")
    private String redisPassword;
    /*集群状态扫描间隔时间，单位:毫秒*/
    @Value("${redis.node.interval:5000}")
    private int interval;

    @Value("${redis.slave.node.address:}")
    private String slaveAddresses;

    /**
     * 单体redis
     *
     * @return
     */
    @ConditionalOnProperty(name = "redis.mode", havingValue = "single")
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + redisAddress + ":" + redisPort)
                .setPassword(redisPassword);

        return Redisson.create(config);
    }

    /**
     * 主从redis
     *
     * @return
     */
    @ConditionalOnProperty(name = "redis.mode", havingValue = "master-slave")
    @Bean
    public RedissonClient redissonClientByMasterSlave() {
        Config config = new Config();
        MasterSlaveServersConfig masterSlaveServersConfig = config.useMasterSlaveServers()
                .setMasterAddress("redis://" + redisAddress + ":" + redisPort)
                .setPassword(redisPassword)
                .setReadMode(ReadMode.MASTER_SLAVE);

        if (null != slaveAddresses && slaveAddresses.length() > 0) {
            String[] slaveArray = slaveAddresses.split(";");
            if (null != slaveArray) {
                for (String slaveAddress : slaveArray) {
                    masterSlaveServersConfig.addSlaveAddress(slaveAddress);
                }
            }
        }

        return Redisson.create(config);
    }

    /**
     * 集群redis
     *
     * @return
     */
    @ConditionalOnProperty(name = "redis.mode", havingValue = "cluster")
    @Bean
    public RedissonClient redissonClientByCluster() {
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers()
                .setScanInterval(interval)
                .setPassword(redisPassword);
        if (null != redisAddress && redisAddress.length() > 0) {
            String[] nodeAddresses = redisAddress.split(";");
            String[] fullRedisAddresses = new String[nodeAddresses.length];
            for (int i = 0; i < nodeAddresses.length; i++) {
                String nodeAddress = nodeAddresses[i];
                fullRedisAddresses[i] = "redis://" + nodeAddress + ":" + redisPort;
            }
            clusterServersConfig.addNodeAddress(fullRedisAddresses);
        }

        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

}
